//
//  MachineDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/18.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "MachineDetailViewController.h"
#import "SelectItemButton.h"
#import "HttpEquipmentAction.h"
#import "HistoryView.h"
#import "AddMaintainViewController.h"

@interface MachineDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) EquipmentInfo *equipmentInfo;
@property (nonatomic,strong) UIButton *launchButton;
@property (nonatomic,strong) UIButton *repairButton;
@property (nonatomic,strong) UIButton *addressButton;
@property (nonatomic,strong) UIImageView *triangleImageView;
@property (nonatomic,strong) UIView *headV;
@property (nonatomic,strong) UIScrollView *scrollView;


@end

@implementation MachineDetailViewController

-(id)initWithEquipmentInfo:(EquipmentInfo*)equipmentInfo{
    if (self == [super init]) {
        self.equipmentInfo = equipmentInfo;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (![self.equipmentInfo.status isEqualToString:@"maintaining"]) {
        UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        rightbutton.titleLabel.font = [UIFont systemFontOfSize:16];
        [rightbutton setTitle:@"报修" forState:UIControlStateNormal];
        [rightbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(maintainAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    }
    
    [self getHeaderView];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kDevice_Is_iPhoneX?350:325, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-350:SCREEN_HEIGHT-325)];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, 0);
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    for (int i =0; i<2; i++) {
        HistoryView *historyView = [[HistoryView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, scrollView.frameHeight)];
        if (i == 0) {
            [historyView loadOtherData:self.equipmentInfo];

        }else{
            [historyView loadData:self.equipmentInfo block:^(NSString *idString,NSString *status) {
                if (![status isEqualToString:@"maintained"]) {
                    self.equipmentInfo.maintainId = idString;
                    AddMaintainViewController *addMaintainViewController = [[AddMaintainViewController alloc] initWithEquipmentCode:self.equipmentInfo];
                    [self.navigationController pushViewController:addMaintainViewController animated:YES];
                }
                
            }];

        }
        [scrollView addSubview:historyView];
    }
    

}

-(void)maintainAction:(id)sender{
    AddMaintainViewController *addMaintainViewController = [[AddMaintainViewController alloc] initWithEquipmentCode:self.equipmentInfo];
    [self.navigationController pushViewController:addMaintainViewController animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        self.launchButton.selected = YES;
        self.repairButton.selected = NO;
        self.triangleImageView.sd_layout.leftSpaceToView(self.headV, 0);
        [self.triangleImageView updateLayout];
    }else if (scrollView.contentOffset.x == SCREEN_WIDTH){
        self.launchButton.selected = NO;
        self.repairButton.selected = YES;
        self.triangleImageView.sd_layout.leftSpaceToView(self.headV, SCREEN_WIDTH/2);
        [self.triangleImageView updateLayout];
    }
}



-(void)getHeaderView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,kDevice_Is_iPhoneX?350:325)];
    [self.view addSubview:view];
    self.headV = view;
    
    if ([self.equipmentInfo.status isEqualToString:@"open"]) {
        view.backgroundColor = DefaultOpenColor;
    }else if([self.equipmentInfo.status isEqualToString:@"renting"]){
        view.backgroundColor = DefaultTenantColor;
    }else{
        view.backgroundColor = DefaultRepairColor;
        
    }
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = self.equipmentInfo.code;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:20];
    [view addSubview:nameLabel];
    nameLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(view, StatusBarHeight+44+20).heightIs(20).autoWidthRatio(0);
    [nameLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *addressButton = [UIButton new];
    addressButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [addressButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addressButton setTitle:[NSString stringWithFormat:@"%@%@",self.equipmentInfo.city,self.equipmentInfo.district] forState:UIControlStateNormal];
    [addressButton setImage:[UIImage imageNamed:@"machine_local"] forState:UIControlStateNormal];
    addressButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [view addSubview:addressButton];
    addressButton.sd_layout.leftSpaceToView(view, 20).topSpaceToView(nameLabel, 5).widthIs(SCREEN_WIDTH-40).heightIs(20);
    self.addressButton = addressButton;
    
    UILabel *statusLabel = [UILabel new];
    statusLabel.text = @"工作状态:正常";
    statusLabel.textAlignment = NSTextAlignmentRight;
    statusLabel.textColor = [UIColor whiteColor];
    statusLabel.font = [UIFont systemFontOfSize:12];
    [view addSubview:statusLabel];
    statusLabel.sd_layout.rightSpaceToView(view, 20).topSpaceToView(view, StatusBarHeight+44+23).heightIs(20).autoWidthRatio(0);
    [statusLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"太阳能A款";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.rightSpaceToView(view, 20).topSpaceToView(statusLabel, 5).heightIs(20).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(view, 20).rightSpaceToView(view, 20).topSpaceToView(addressButton, 25).heightIs(1);
    
    UILabel *companyLabel = [UILabel new];
    companyLabel.text = self.equipmentInfo.tenantName;
    companyLabel.textAlignment = NSTextAlignmentCenter;
    companyLabel.textColor = [UIColor whiteColor];
    companyLabel.numberOfLines = 0;
    companyLabel.font = [UIFont systemFontOfSize:20];
    [view addSubview:companyLabel];
    companyLabel.sd_layout.leftSpaceToView(view, 0).topSpaceToView(lineView, 15).widthIs(SCREEN_WIDTH).autoHeightRatio(0);
    
    
//    UILabel *timeLabel = [UILabel new];
//
//    timeLabel.text = [NSString stringWithFormat:@"%@到%@",[Utils getTimeFront:self.equipmentInfo.rentStartDate],[Utils getTimeFront:self.equipmentInfo.rentEndDate]];
//    timeLabel.textAlignment = NSTextAlignmentCenter;
//    timeLabel.textColor = [UIColor whiteColor];
//    timeLabel.font = [UIFont systemFontOfSize:14];
//    [view addSubview:timeLabel];
//    timeLabel.sd_layout.leftSpaceToView(view, 0).topSpaceToView(companyLabel, 5).heightIs(14).widthIs(SCREEN_WIDTH);
    
    
   
    UIView *lineView1 = [UIView new];
    lineView1.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView1];
    if (self.equipmentInfo.agentName.length>0) {
        companyLabel.text = [NSString stringWithFormat:@"代理商\n%@",self.equipmentInfo.agentName];
    }else{
        companyLabel.text = @"暂无代理商";

    }
//    if ([self.equipmentInfo.status isEqualToString:@"open"]) {
//        companyLabel.text = @"空闲中";
//        companyLabel.sd_layout.topSpaceToView(lineView, 30);
//        timeLabel.hidden = YES;
//    }else if ([self.equipmentInfo.status isEqualToString:@"maintaining"]){
//        companyLabel.text = @"维修中";
//        companyLabel.sd_layout.topSpaceToView(lineView, 30);
//        timeLabel.hidden = YES;
//    }
    lineView1.sd_layout.leftSpaceToView(view, 20).rightSpaceToView(view, 20).topSpaceToView(companyLabel, 15).heightIs(1);
    
    UIButton *button1 = [UIButton new];
    button1.titleLabel.font = [UIFont systemFontOfSize:14];
    [button1 setImage:[UIImage imageNamed:@"m_history"] forState:UIControlStateNormal];
    [button1 setImage:[UIImage imageNamed:@"m_history_select"] forState:UIControlStateSelected];
    [button1 setTitle:@"投放历史" forState:UIControlStateNormal];
    button1.imageEdgeInsets = UIEdgeInsetsMake(-15, 30, 15, -30);
    button1.titleEdgeInsets = UIEdgeInsetsMake(15, -10, -15, 10);
    [button1 addTarget:self action:@selector(historyAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];
    button1.sd_layout.leftSpaceToView(view, 0).topSpaceToView(lineView1, 0).widthIs(SCREEN_WIDTH/2).bottomSpaceToView(view, 0);
    button1.selected = YES;
    self.launchButton = button1;
    
    UIButton *button2 = [UIButton new];
    button2.titleLabel.font = [UIFont systemFontOfSize:14];
    [button2 setImage:[UIImage imageNamed:@"m_wx"] forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"m_wx_select"] forState:UIControlStateSelected];
    [button2 setTitle:@"维修历史" forState:UIControlStateNormal];
    button2.imageEdgeInsets = UIEdgeInsetsMake(-15, 30, 15, -30);
    button2.titleEdgeInsets = UIEdgeInsetsMake(15, -10, -15, 10);
    [button2 addTarget:self action:@selector(historyAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button2];
    button2.sd_layout.leftSpaceToView(button1, 0).topSpaceToView(lineView1, 0).widthIs(SCREEN_WIDTH/2).bottomSpaceToView(view, 0);
    self.repairButton = button2;
    
    UIImageView *triangleImageView = [UIImageView new];
    triangleImageView.contentMode = UIViewContentModeCenter;
    triangleImageView.image = [UIImage imageNamed:@"sanjiao"];
    [view addSubview:triangleImageView];
    triangleImageView.sd_layout.bottomSpaceToView(view, 0).leftSpaceToView(view, 0).widthIs(SCREEN_WIDTH/2).heightIs(10);
    self.triangleImageView = triangleImageView;
    
}

-(void)historyAction:(id)sender{
    UIButton *button = sender;
    if (button == self.launchButton) {
        self.launchButton.selected = YES;
        self.repairButton.selected = NO;
        self.triangleImageView.sd_layout.leftSpaceToView(self.headV, 0);
        [self.triangleImageView updateLayout];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        self.launchButton.selected = NO;
        self.repairButton.selected = YES;
        self.triangleImageView.sd_layout.leftSpaceToView(self.headV, SCREEN_WIDTH/2);
        [self.triangleImageView updateLayout];
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
